package com.servlet;

import com.beans.Cateinfo;
import com.dao.CateDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/CateServlet")
public class CateServlet extends HttpServlet {
    CateDao cateDao= new CateDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String flag = request.getParameter("flag");
        if ("checkaddcate".equals(flag)){
            checkaddcate(request,response);
        }else if("addbigcate".equals(flag)){
            addbigcate(request,response);
        }else if("smalladd".equals(flag)){
            smalladd(request,response);
        }else if("manage".equals(flag)){
            manage(request,response);
        }else if("addsmallcate".equals(flag)){
            addsmallcate(request,response);
        }else if("bupdate".equals(flag)){
            bupdate(request,response);
        }else if("supdate".equals(flag)){
            supdate(request,response);
        }else if("bigupdate".equals(flag)){
            bigupdate(request,response);
        }else if("smallupdate".equals(flag)){
            smallupdate(request,response);
        }else if("del".equals(flag)){
            del(request,response);
        }


    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        cateDao.del(id);
        manage(request,response);
    }

    private void smallupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        int parentId=Integer.parseInt(request.getParameter("parentId"));
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");

        Cateinfo cateinfo= new Cateinfo();
        cateinfo.setId(id);
        cateinfo.setCateName(cateName);
        cateinfo.setDes(des);
        cateinfo.setParentId(parentId);
        int n=cateDao.supdate(cateinfo);
        List<Cateinfo> blist= cateDao.getslist();
        if(n>0){
            request.setAttribute("blist",blist);
            request.setAttribute("cateinfo",cateinfo);
            request.setAttribute("msg","修改成功");
            request.getRequestDispatcher("/cate/small-update.jsp").forward(request, response);
        }
    }

    private void bigupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        Cateinfo cateinfo= new Cateinfo();
        cateinfo.setId(id);
        cateinfo.setCateName(cateName);
        cateinfo.setDes(des);
        int n=cateDao.bupdate(cateinfo);
        if(n>0){
            request.setAttribute("cateinfo",cateinfo);
            request.setAttribute("msg","修改成功");
            request.getRequestDispatcher("/cate/big-update.jsp").forward(request, response);
        }
    }

    private void supdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Cateinfo cateinfo=cateDao.getscatebyid(id);
        request.setAttribute("cateinfo",cateinfo);
        request.getRequestDispatcher("/cate/small-update.jsp").forward(request, response);
    }

    private void bupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        Cateinfo cateinfo=cateDao.getbcatebyid(id);
        request.setAttribute("cateinfo",cateinfo);
        request.getRequestDispatcher("/cate/big-update.jsp").forward(request, response);
    }

    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Cateinfo> blist= cateDao.getslist();
        request.setAttribute("blist",blist);
        System.out.println(blist);
        request.getRequestDispatcher("/cate/Cate-manage.jsp").forward(request, response);
    }

    private void smalladd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Cateinfo> blist= cateDao.getslist();
        request.setAttribute("blist",blist);
        request.getRequestDispatcher("/cate/smallCate-add.jsp").forward(request, response);
    }

    private void addbigcate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        Cateinfo cateinfo=new Cateinfo();
        cateinfo.setCateName(cateName);
        cateinfo.setDes(des);
        cateinfo.setParentId(0);
        int i=cateDao.badd(cateinfo);
        if(i>0){
            request.setAttribute("cateName",cateName);
            request.setAttribute("des",des);
            request.setAttribute("msg","添加成功");
            request.getRequestDispatcher("/cate/bigCate-add.jsp").forward(request, response);
        }
    }
    private void addsmallcate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cateName=request.getParameter("cateName");
        String des=request.getParameter("des");
        int parentId=Integer.parseInt(request.getParameter("parentId"));
        Cateinfo cateinfo=new Cateinfo();
        cateinfo.setCateName(cateName);
        cateinfo.setDes(des);
        cateinfo.setParentId(parentId);
        int i=cateDao.sadd(cateinfo);
        if(i>0){
            request.setAttribute("cateName",cateName);
            request.setAttribute("des",des);
            request.setAttribute("parentId",parentId);
            request.setAttribute("msg","添加成功");
            request.getRequestDispatcher("/cate/smallCate-add.jsp").forward(request, response);
        }
    }

    private void checkaddcate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name= (String)request.getParameter("cateName");
        Cateinfo cateinfo= cateDao.getAdminNamebyid(name);
        if(cateinfo==null)
            response.getWriter().print(0);
        else
            response.getWriter().print(1);
    }
}
